#include "cybertron/sensor/noise/PinkNoise.hpp"
#include <algorithm>

CYBERTRON_BEGIN

PinkNoise::PinkNoise(float correctionTime/* = 100*/, float sigma/* = 1.0*/, float initialValue/* = 0.0*/)
	:TimeNoise(EPinkNoise), m_correctionTime(correctionTime), m_whiteNoise(0.0, sigma)
{
	m_value = vec3(initialValue);
}

PinkNoise::PinkNoise(float correctionTime, float sigma, float sigmaAltitude, float initialValue)
	: TimeNoise(EPinkNoise), m_correctionTime(correctionTime), m_whiteNoise(0.0, sigma, 0.0, sigmaAltitude)
{
	m_value = vec3(initialValue);
}

PinkNoise::~PinkNoise()
{
}

vec3 PinkNoise::update() {
	float delta = (float)std::max(0.001, elapsedTime());
	resetTime();
	float alpha = std::exp(-delta / m_correctionTime);
	m_value = m_value * alpha + m_whiteNoise.update() / m_correctionTime;
	m_value.x = std::max(-m_whiteNoise.getSigma(0), std::min(m_value.x, m_whiteNoise.getSigma(0)));
	m_value.y = std::max(-m_whiteNoise.getSigma(1), std::min(m_value.y, m_whiteNoise.getSigma(1)));
	m_value.z = std::max(-m_whiteNoise.getSigma(2), std::min(m_value.z, m_whiteNoise.getSigma(2)));
	return m_value;
}

CYBERTRON_END